package com.example.work;

import com.alibaba.fastjson.JSON;
import com.ql.util.express.DefaultContext;
import com.ql.util.express.ExpressRunner;
import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.stream.Collectors;

/* ━━━━━━佛祖保佑━━━━━━
 *                  ,;,,;
 *                ,;;'(    社
 *      __      ,;;' ' \   会
 *   /'  '\'~~'~' \ /'\.)  主
 * ,;(      )    /  |.     义
 *,;' \    /-.,,(   ) \    码
 *     ) /       ) / )|    农
 *     ||        ||  \)
 *     (_\       (_\
 * ━━━━━━永无BUG━━━━━━
 * @author ：zjq
 * @date ：2021/5/30 19:06
 * @description: TODO
 * @version: V1.0
 * @slogan: 天下风云出我辈，一入代码岁月催
 */
@Slf4j
public class QlExpreTest {
    public static void main(String[] args) throws Exception {





        String ruleExpre = "(GZIntory+DSHIntory)*DSJIntory>5000";

        Map<String, Map<String, Object>> acquireMap = new HashMap<>();

        Map<String, Object> GZ = new HashMap<>();
        GZ.put("sku0001", 10);
        GZ.put("sku0002", 20);
        GZ.put("sku0003", 30);

        Map<String, Object> DSH = new HashMap<>();
        DSH.put("sku0001", 100);
        DSH.put("sku0002", 200);
        DSH.put("sku0003", 300);

        Map<String, Object> DSJ = new HashMap<>();
        DSJ.put("sku0001", 12);
        DSJ.put("sku0002", 13);
        DSJ.put("sku0003", 14);

        acquireMap.put("GZIntory", GZ);
        acquireMap.put("DSHIntory", DSH);
        acquireMap.put("DSJIntory", DSJ);

        List<String> skuCodeList = new ArrayList<>();
        skuCodeList.add("sku0001");
        skuCodeList.add("sku0002");
        skuCodeList.add("sku0003");
        System.out.println("=============================================");
        System.out.println("=============================================");
        System.out.println("=============================================");

        // 将以 指标分组的 变成以 skucode 分组 调用ql表达式
        List<IntoryItem> list = new ArrayList<>();
        // k : 指标名   v : map
        acquireMap.forEach((k, v) -> {
            String intoryName = k;  //  指标名
            // kk:sku   vv 值
            v.forEach((kk, vv) -> {
                IntoryItem item = IntoryItem.builder().skuCode(kk).intoryName(k).value((int) vv).build();
                list.add(item);
            });
        });

        Map<String, List<IntoryItem>> newMap = list.stream().collect(Collectors.groupingBy(IntoryItem::getSkuCode));

        // 满足条件的skuCode
        List<IntoryItem> skuCodeInUse = new ArrayList<>();

        ExpressRunner runner = new ExpressRunner();
        DefaultContext<String, Object> context = new DefaultContext<String, Object>();
        newMap.forEach((k, v) -> {
            for (IntoryItem i : v) {
                // 放入规则
                context.put(i.getIntoryName(), i.getValue());
            }
            //===> 定义规则，计算表达式等等
            String express = ruleExpre;
            // 解析规则+执行规则
            Object r = null;
            try {
                r = runner.execute(express, context, null, true, true);
                log.info("当前的 skucCode 是 :{}, 计算结果为 :{}", k, r);
                IntoryItem build = IntoryItem.builder().skuCode(k).calcuResult((boolean)r).build();
                skuCodeInUse.add(build);
            } catch (Exception e) {
                e.printStackTrace();
            }

//            System.out.println(r);
        });

        skuCodeInUse.stream().forEach(n -> {
            System.out.println();
        });

    }
}
